<?php
/**
 * ShopCartModel.class.php
 * 购物车模型
 *
 * @author   <王中艺>
 * @date 	2017-02-06
 */

namespace Wechat\Model;

class ShopCartModel extends BaseModel{

    //重定义数据表明
	protected   $trueTableName = 't_shop_cart';

    /**
     * 添加购物车信息
     * @param   $userId 用户ID
     * @param   $goodsType 物品类型
     * @param   $goodsIds 物品ID集合
     * @param   $amount 金额
     */
    function addCartInfo($userId, $goodsType, $goodsIds, $amount){
    	$cartInfo 	= array(
    		'user_id'	=> $userId,
    		'goods_type'	=> $goodsType,
    		'goods_ids'	=> implode(',',$goodsIds),
    		'amount'	=> $amount,
    		'create_time'	=> time(),
    		'pay_time'	=> 0,
    		'status'	=> 0
    	);
        
    	return $this->add($cartInfo);
    }

    /**
     * 购物车支付成功
     * @param   $payInfo 支付信息
     * @return  boolean 
     */
    function paySuccess($payInfo){
        $cartId     = $payInfo['goods_id'];
    	$cartInfo 	= $this->where('id = %d', $cartId)->find();
    	if(!$cartInfo){
    		$this->errMsg 	= '未找到购物车信息:CartId:'.$cartId;
    		return false;
    	}
    	//物品类型处理
    	switch($cartInfo['goods_type']){
    		//课程处理
    		case 1:
    			$courseIds 	= explode(',', $cartInfo['goods_ids']);
                $courseInfos    = M('t_course', null)
                    ->where('id in (%s)', $cartInfo['goods_ids'])
                    ->cache(true)
                    ->getField('id, price, name');

                $cartInfo['goods_infos'] = json_encode($courseInfos);
                $cartInfo['price']  = 0;

    			$userCourses 	= array();
    			foreach($courseIds as $courseId){
    				$userCourses[] 	= array(
    					'user_id'	=> $cartInfo['user_id'],
    					'course_id'	=> $courseId,
    					'cart_shop_id'	=> $cartId,
                        'price' => $courseInfos[$courseId]['price'],
    					'create_time'	=> time(),
    					'get_type'	=> 3,
                        'get_relate_id' => $cartId,
    					'status'	=> 1
    				);
                    $cartInfo['price']  += $courseInfos[$courseId]['price'];
    			}
    			$ret 	= M('t_user_course', null)->addAll($userCourses);
    			if($ret === false){
    				$this->errMsg 	= '用户课程添加失败!CartId:'.$cartId;
    				return false;
    			}
                
                //消息发送
                $wxTempMsg  = \Wechat\Util\WxTempMsg::getInstance();
                $userInfo   = D('User')->getUserInfo($cartInfo['user_id']);
                $courses    = M('t_course', null)->where('id in (%s)', implode(',', $courseIds))->select();
                foreach($courses as $courseInfo){
                    $wxTempMsg->buyCourse($userInfo, $courseInfo, $payInfo);
                }
    		break;

    		default: return true; break;
    	}

    	$cartInfo['status']	= 1;
        $cartInfo['money_id']   = $payInfo['id'];
    	$cartInfo['pay_time']	= time();
    	$ret 	= $this->save($cartInfo);
    	if($ret === false){
    		$this->errMsg 	= '购物车信息更改失败!CartId:'.$cartId;
    		return false;
    	}
    	return true;
    }
}